package com.itheima.d3_udp2;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.util.Scanner;

/**
 * UDP客户端：发s 送数据  多发多收
 */
public class Client {
    public static void main(String[] args) throws Exception {
        //1.创建发送端对象
        DatagramSocket socket = new DatagramSocket(10000);

        Scanner sc = new Scanner(System.in);
        //2.创建数据，并把数据打包
        while (true) {
            System.out.println("请说：");
            String msg = sc.next();//发送的数据
            byte[] bytes = msg.getBytes();
            //判断什么时候关闭
            if("exit".equals( msg)){
                //服务器退出
                System.out.println("====客户端退出====");
                socket.close();
                break;
            }
            DatagramPacket packet = new DatagramPacket(bytes, bytes.length, InetAddress.getLocalHost(), 10001);
            //3.发送数据
            socket.send(packet);


        }
    }
}
